import 'package:dio/dio.dart';
import 'package:distribution_mall_flutter/base/base_service.dart';
import 'package:distribution_mall_flutter/constant/constant.dart';
import 'package:distribution_mall_flutter/constant/point_constant.dart';
import 'package:distribution_mall_flutter/constant/status_constant.dart';
import 'package:distribution_mall_flutter/dialog/merchant_auth_dialog.dart';
import 'package:distribution_mall_flutter/dialog/two_result_hint_dialog.dart';
import 'package:distribution_mall_flutter/generated/l10n.dart';
import 'package:distribution_mall_flutter/http/modules/merchant_authentication.dart';
import 'package:distribution_mall_flutter/provider/order/order_detail.dart';
import 'package:distribution_mall_flutter/router/route_utils.dart';
import 'package:distribution_mall_flutter/utils/firebase/firebase_analytics_utils.dart';
import 'package:distribution_mall_flutter/router/router.dart';
import 'package:distribution_mall_flutter/router/router.gr.dart';
import 'package:flutter/material.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'order_detail_service.g.dart';

class OrderDetailService extends BaseService {
  final String distributionOrderId;

  OrderDetailService(super.ref, this.distributionOrderId);

  @override
  void onReady() async {
    if (distributionOrderId.isEmpty) return;
    await ref
        .read(orderDetailProvider.notifier)
        .getOrderDetail(distributionOrderId);

    ///不是领样商品不弹出认证弹框
    if (ref.read(orderDetailProvider).detal?.orderType !=
        OrderTypeConstant.sample) {
      return;
    }
    CancelToken firstCancelToken = CancelToken();
    CancelToken secondCancelToken = CancelToken();
    ref.onDispose(() {
      firstCancelToken.cancel();
      secondCancelToken.cancel();
    });
    (await MerchantAuthenticationApi.queryLast(cancelToken: firstCancelToken))
        .yes((value) async {
      ref
          .read(orderDetailMerchantAuthProvider.notifier)
          .setNewData(value?.auditState);
      if (value == null || value.auditState == 'REJECT') {
        var startTime = DateTime.now();

        ///去弹框提示
        bool? result = await showDialog(
            context: appRouter.navigatorKey.currentContext!,
            builder: (_) {
              return MerchantAuthDialog(
                title: S.current.order_merchant_verification_hint,
                leftButton: S.current.cancel,
                rightButton: S.current.go,
              );
            });
        RouteUtils.onStoreDialogStayTime(
          name: PointDialogNameConstant.orderMerchantAuthenticationDialog,
          startTime: startTime,
          endTime: DateTime.now(),
        );
        if (result == true) {
          FirebaseAnalyticsUtils.logEvent(
              name: PointConstant.orderMerchantAuthenticationDialogGo,
              parameters: {'verification_status': value == null ? 1 : 2});
        }
        if (result == false) {
          bool? exitResult = await showDialog(
              context: appRouter.navigatorKey.currentContext!,
              builder: (_) {
                return TwoResultHintDialog(
                  title: S.current.order_merchant_verification_exit_hint,
                  rightText: S.current.yes,
                );
              });
          if (exitResult == true) {
            return;
          } else if (exitResult == false) {
            result = true;
          }
        }
        if (result == true) {
          await appRouter.push(const MerchantAuthenticationRoute());
          (await MerchantAuthenticationApi.queryLast(
                  cancelToken: secondCancelToken))
              .yes((value) async {
            ref
                .read(orderDetailMerchantAuthProvider.notifier)
                .setNewData(value?.auditState);
          });
        }
      } else if (value.auditState == 'REVIEW') {
        showDialog(
            context: appRouter.navigatorKey.currentContext!,
            builder: (_) {
              return MerchantAuthDialog(
                title: S.current.order_merchant_verification_review_hint,
                rightButton: S.current.ok,
              );
            });
      }
    });
  }
}

@riverpod
OrderDetailService orderDetailService(
    OrderDetailServiceRef ref, String distributionOrderId) {
  return OrderDetailService(ref, distributionOrderId);
}
